package com.baidumap.administrator.happystudy.ui;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.ab.dialog.loadingDialog.LoadingDialogUtil;
import com.ab.http.OnMessageResponse;
import com.ab.util.AbStrUtil;
import com.ab.util.TimeCountUtil;
import com.ab.util.ToastUtil;
import com.ab.view.clear_edit_text.ClearEditText;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.api.Api;
import com.baidumap.administrator.happystudy.base.AppBaseActivity;
import com.baidumap.administrator.happystudy.base.Constant;
import com.baidumap.administrator.happystudy.model.ChangePwd;
import com.baidumap.administrator.happystudy.model.SendSMS;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 描述：设置密码
 * 作者：xiaoxing on 17/4/7 15:11
 * 邮箱：2235445233@qq.com
 */
public class ModifyPwdActivity2 extends AppBaseActivity implements OnMessageResponse {

    @Bind(R.id.cdt_yan_zheng_ma)
    ClearEditText cdtYanZhengMa;
    @Bind(R.id.cet_pwd)
    ClearEditText cetPwd;
    @Bind(R.id.btn_countdown)
    TextView btnCountdown;


    private String mTel, mVerityCode, mPwd;

    @Override
    public int bindLayout() {
        return R.layout.activity_modify_pwd_2;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
    }


    @OnClick({R.id.btn_kai_qi, R.id.btn_countdown})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_kai_qi:

                register();
                break;
            case R.id.btn_countdown:

                reCountDown();
                break;
        }
    }

    /**
     * 执行注册
     */
    private void register() {
        if (checkCodePwd()) {


            LoadingDialogUtil.showGifdialog(getSupportFragmentManager());
            Api.changePassword(this, sHelper.getString(Constant.UID), mPwd, mTel, mVerityCode);

        }
    }

    /**
     * 检查验证码和密码
     *
     * @return
     */
    private boolean checkCodePwd() {
        mVerityCode = getEidtTextValue(cdtYanZhengMa);
        mPwd = getEidtTextValue(cetPwd);

        if (AbStrUtil.isEmpty(mVerityCode)) {
            ToastUtil.showMessage(this, "验证码不能为空");
            return false;
        } else if (AbStrUtil.isEmpty(mPwd)) {
            ToastUtil.showMessage(this, "密码不能为空");
            return false;
        } else if (mPwd.length() < 6) {
            ToastUtil.showMessage(this, "密码不能少于6位");
            return false;
        }

        return true;
    }

    @Override
    public void doBusiness(Context mContext) {
        super.doBusiness(mContext);
        setHeaderBack();
        setHeaderTitle(R.string.modify_pwd);
        getBundleValue();

    }

    /**
     * 获取bundleValue
     */
    private void getBundleValue() {

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            mTel = bundle.getString("tel");
            countDown();
        }

    }

    /**
     * 开始计时
     */
    private void countDown() {
        new TimeCountUtil(btnCountdown, Constant.SECURITY_CODE_TIME, 1000).start();
    }

    /**
     * 重新发送
     */
    private void reCountDown() {
        new TimeCountUtil(btnCountdown, Constant.SECURITY_CODE_TIME, 1000).start();
        LoadingDialogUtil.showGifdialog(getSupportFragmentManager());
        Api.sendCheckCode(this, mTel);
    }

    @Override
    public void onMessageResponse(String url, JSONObject jo) throws JSONException {

        if (url.equals(Constant.CHANGE_PASSWORD) && jo != null) {
            ChangePwd changePwd = new ChangePwd(jo.toString());

            if (changePwd != null) {


                if (changePwd.getCode().equals("200")) {

                    ToastUtil.showMessage(this, "密码修改成功");

                    startActivity(LoginActivity.class);
                    finish();
                    sHelper.removeString(Constant.PASSWORD);
                } else {

                    ToastUtil.showMessage(this, changePwd.getMsg());

                }
            }

        }
        if (url.equals(Constant.SENE_CHECK_CODE) && jo != null) {
            SendSMS sendSMS = new SendSMS(jo.toString());

            if (sendSMS != null) {


                if (sendSMS.getCode().equals("200")) {

                    ToastUtil.showMessage(this, "验证码发送成功");
                } else {

                    ToastUtil.showMessage(this, sendSMS.getMsg());

                }
            }

        }
    }

    @Override
    public void onMessageResponse(String url, JSONArray jo) throws JSONException {

    }

    @Override
    public void onMessageResponse(String url, String str) throws Exception {

    }
}
